home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / text / hyper / hsc_source.lha / hsc / source / hsclib / entity.c < prev    next >
C/C++ Source or Header  |  1996-10-13  |  4KB  |  222 lines

  1. /*
  2.  * entity.c
  3.  *
  4.  * entity structure, variables and functions ( "&xx;")
  5.  *
  6.  * Copyright (C) 1995,96  Thomas Aglassinger
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * updated: 13-Oct-1996
  23.  * created:  8-Sep-1995
  24.  *
  25.  */
  26.  
  27. #define NOEXTERN_HSCLIB_ENTITY_H
  28. #include "hsclib/inc_base.h"
  29. #include "hsclib/entity.h"
  30.  
  31. /*
  32.  *-------------------------------------
  33.  * constructor/destructor
  34.  *-------------------------------------
  35.  */
  36.  
  37. /*
  38.  * new_hscent
  39.  *
  40.  * alloc & init a new hscentity
  41.  */
  42. HSCENT *new_hscent(STRPTR newid)
  43. {
  44.     HSCENT *newent = (HSCENT *) umalloc(sizeof(HSCENT));
  45.  
  46. #if DEBUG_ENTITY
  47.     fprintf(stderr, DHL "   new_enty %s\n", newid);
  48. #endif
  49.  
  50.     if (newent)
  51.     {
  52.         /* init new entity item */
  53.         newent->name = strclone(newid);         /* set id */
  54.         newent->replace = NULL;
  55.         newent->numeric = 0L;
  56.     }
  57.  
  58.     return (newent);
  59.  
  60. }
  61.  
  62. /*
  63.  * del_entity
  64.  */
  65. VOID del_entity(APTR data)
  66. {
  67.     HSCENT *ent = (HSCENT *) data;
  68.  
  69. #if DEBUG_ENTITY
  70.     fprintf(stderr, DHL "   del_enty %s\n", ent->name);
  71. #endif
  72.  
  73.     ufreestr(ent->name);
  74.     ufreestr(ent->replace);
  75.     ent->numeric = 0;
  76.     ufree(ent);
  77. }
  78.  
  79. /*
  80.  * cpy_hscent
  81.  *
  82.  * create a copy of an already existing entity
  83.  */
  84. HSCENT *cpy_hscent(HSCENT * oldent)
  85. {
  86.     HSCENT *newent = new_hscent(oldent->name);
  87.  
  88.     if (newent)
  89.     {
  90.         newent->replace = strclone(oldent->replace);
  91.         newent->numeric = oldent->numeric;
  92.     }
  93.  
  94.     return (newent);
  95. }
  96.  
  97. /*
  98.  *---------------------------
  99.  * find entity string
  100.  *---------------------------
  101.  */
  102.  
  103. /*
  104.  * cmp_strent
  105.  *
  106.  * compares a entity-string with the name
  107.  * of a HSCENT-entry
  108.  */
  109. int cmp_strent(APTR cmpstr, APTR entdata)
  110. {
  111.     STRPTR entstr = NULL;
  112.  
  113.     if (entdata)
  114.         entstr = ((HSCENT *) entdata)->name;
  115.  
  116.     if (entstr)
  117.         if (!strcmp(cmpstr, entstr))
  118.             return (-1);
  119.         else
  120.             return (0);
  121.     else
  122.         return (0);
  123. }
  124.  
  125. /*
  126.  * cmp_nument
  127.  *
  128.  * compares a entity-string with the numeric
  129.  * data of a HSCENT-entry
  130.  */
  131. int cmp_nument(APTR cmpstr, APTR entdata)
  132. {
  133.     LONG num = -1;
  134.     LONG cmpnum = (LONG) cmpstr;
  135.  
  136.     if (entdata)
  137.         num = ((HSCENT *) entdata)->numeric;
  138.  
  139.     return ((num != -1) && (num == cmpnum));
  140. }
  141.  
  142. /*
  143.  * cmp_rplcent
  144.  *
  145.  * compares a entity-string with the replace-item
  146.  * of a HSCENT-entry
  147.  */
  148. int cmp_rplcent(APTR cmpstr, APTR entdata)
  149. {
  150.     STRPTR entstr = NULL;
  151.  
  152.     if (entdata)
  153.         entstr = ((HSCENT *) entdata)->replace;
  154.  
  155.     if (entstr)
  156.         if (!strcmp(cmpstr, entstr))
  157.             return (-1);
  158.         else
  159.             return (0);
  160.     else
  161.         return (0);
  162. }
  163.  
  164. /*
  165.  *-------------------------------------
  166.  * append entity functions
  167.  *-------------------------------------
  168.  */
  169.  
  170. /*
  171.  * app_entnode
  172.  *
  173.  * create a new entity and append it to entity-list
  174.  *
  175.  * params: entid..name of the new entity (eg "uuml")
  176.  * result: ptr to the new entity or NULL if no mem
  177.  */
  178. HSCENT *app_entnode(DLLIST * entlist, STRPTR entid)
  179. {
  180.     HSCENT *newent;
  181.  
  182.     newent = new_hscent(entid);
  183.     if (newent)
  184.     {
  185.         if (app_dlnode(entlist, newent) == NULL)
  186.         {
  187.             del_entity((APTR) newent);
  188.             newent = NULL;
  189.         }
  190.     }
  191.  
  192.     return (newent);
  193. }
  194.  
  195. /*
  196.  * add_ent
  197.  */
  198. BOOL add_ent(DLLIST * entlist, STRPTR entid, STRPTR entreplace, LONG num)
  199. {
  200.     HSCENT *newent = app_entnode(entlist, entid);
  201.  
  202.     if (entreplace)
  203.         newent->replace = strclone(entreplace);
  204.  
  205.     newent->numeric = num;
  206.  
  207. #if 0
  208.     DDE(
  209.            {
  210.            STRPTR rplc = entreplace;
  211.            if (!rplc)
  212.            rplc = "(empty)";
  213.            fprintf(stderr, DHL "defent: \"%s\" \"%s\" %ld\n",
  214.                    entid, rplc, num);
  215.            }
  216.     );
  217. #endif
  218.  
  219.     return (TRUE);
  220. }
  221.  
  222.